package com.ruoyi.web.controller.application;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.ApplicationUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.framework.shiro.util.AuthorizationUtils;
import com.ruoyi.system.service.IApplicationUserService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysPostService;
import com.ruoyi.system.service.ISysRoleService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 报名用户信息
 *
 * @author ruoyi
 */
@Controller
@RequestMapping("/application/user")
public class ApplicationUserController extends BaseController {

    private String prefix = "application/user";

    @Autowired
    private IApplicationUserService applicationUserService;

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISysPostService postService;

    @Autowired
    private SysPasswordService passwordService;

    @RequiresPermissions("application:user:view")
    @GetMapping()
    public String user() {
        return prefix + "/user";
    }

    @RequiresPermissions("application:user:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ApplicationUser user) {
        startPage();
        List<ApplicationUser> list = applicationUserService.selectApplicationInfoList(user);
        return getDataTable(list);
    }

    @Log(title = "报名用户管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("application:user:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ApplicationUser user) {
        List<ApplicationUser> list = applicationUserService.selectApplicationInfoList(user);
        ExcelUtil<ApplicationUser> util = new ExcelUtil<ApplicationUser>(ApplicationUser.class);
        return util.exportExcel(list, "报名用户数据");
    }

    @Log(title = "报名用户管理", businessType = BusinessType.IMPORT)
    @RequiresPermissions("application:user:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ApplicationUser> util = new ExcelUtil<ApplicationUser>(ApplicationUser.class);
        List<ApplicationUser> userList = util.importExcel(file.getInputStream());
        String message = applicationUserService.importApplicationUser(userList, updateSupport, getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("application:user:view")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate() {
        ExcelUtil<ApplicationUser> util = new ExcelUtil<ApplicationUser>(ApplicationUser.class);
        return util.importTemplateExcel("报名用户数据");
    }

    /**
     * 新增报名用户
     */
    @GetMapping("/add")
    public String add(ModelMap mmap) {
        return prefix + "/add";
    }

    /**
     * 新增保存报名用户
     */
    @RequiresPermissions("application:user:add")
    @Log(title = "报名用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated ApplicationUser user) {
        if (!applicationUserService.checkEmailUnique(user)) {
            return error("新增报名用户'" + user.getApplicationUserName() + "'失败，登录账号已存在");
        }
        user.setCreateBy(getLoginName());
        return toAjax(applicationUserService.insertApplicationInfo(user));
    }

    /**
     * 修改保存报告
     */
    @RequiresPermissions("application:user:edit")
    @Log(title = "报名用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated ApplicationUser user) {
        user.setUpdateBy(getLoginName());
        AuthorizationUtils.clearAllCachedAuthorizationInfo();
        return toAjax(applicationUserService.updateApplicationInfo(user));
    }


    @RequiresPermissions("application:user:remove")
    @Log(title = "报名用户管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(Long[] ids) {
        return toAjax(applicationUserService.deleteApplicationInfoByIds(ids));
    }


}